package com.example.lets;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.github.ksoichiro.android.observablescrollview.ObservableListView;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks;
import com.github.ksoichiro.android.observablescrollview.ScrollState;


public class ScrollViewActivity extends AppCompatActivity
        implements ObservableScrollViewCallbacks {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
        }
        catch (Exception e) {
            e.getMessage();
        }
        setContentView(R.layout.activity_scroll_view);

        ObservableListView listView = (ObservableListView) findViewById(R.id.list);
        listView.setScrollViewCallbacks(this);
        String[] str = {"abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc",
                "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc"};
        ScrollAdapter sa = new ScrollAdapter(this, str);
        listView.setAdapter(sa);
    }

    @Override
    public void onScrollChanged(int scrollY, boolean firstScroll,
                                boolean dragging) {
    }

    @Override
    public void onDownMotionEvent() {

    }

    @Override
    public void onUpOrCancelMotionEvent(ScrollState scrollState) {
        ActionBar ab = getSupportActionBar();
        if (scrollState == ScrollState.UP) {
            if (ab.isShowing()) {
                ab.hide();
            }
        } else if (scrollState == ScrollState.DOWN) {
            if (!ab.isShowing()) {
                ab.show();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_scroll_view, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        finish();
        return;
    }
}

class ScrollAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private String[] list;
    private Context context;

    public ScrollAdapter(Context context, String[] str) {
        this.mInflater = LayoutInflater.from(context);
        this.list = str;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ScrollViewHolder holder;

        Log.v("MyListViewBase", "getView " + position + " " + convertView);
        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.listview_item, null);
            holder = new ScrollViewHolder();

            // ???????????????
            holder.title = (TextView)  convertView.findViewById(R.id.ItemTitle);

            // ??ViewHolder????
            convertView.setTag(holder);
        }
        else{
            holder = (ScrollViewHolder) convertView.getTag();
        }

        holder.title.setText(list[position]);

        return convertView;
    }
}

// ?????
class ScrollViewHolder {
    public TextView  title;
}
